import axios from 'axios';

export async function ModelList(userid) {
  return axios.get(`http://localhost:8080/Model/list?userid=${userid}`)
    .then(response => response.data);
}

export async function ModelTrain(record) {
  return axios({
    method:"post",
    contentType:"application/json;charset=UTF-8",
    dataType:"application/json;charset=UTF-8",
    url:"http://localhost:8080/Model/Train",
    data: record
  })
}

export async function ModelDownload(record) {
  return axios({
    method:"post",
    responseType: 'blob',
    url:"http://localhost:8080/Model/download",
    data: record
  }).then(function (res){
    if (!res.data) {
      return
    }


    let url = window.URL.createObjectURL(new Blob([res.data]))
    let link = document.createElement('a')
    link.style.display = 'none'
    link.href = url

    link.setAttribute('download',record.fileName)
    document.body.appendChild(link)
    link.click()
    // 释放URL对象所占资源
    window.URL.revokeObjectURL(url)
    // 用完即删
    document.body.removeChild(link)
  })
}

// 解析内容头中的文件名

export async function ModelDelete(record) {
  return axios({
    method:"delete",
    url:"http://localhost:8080/Model/delete",
    data: record
  })
}

export async function PredictedData(ModelAndTestName) {
  return axios({
    method:"post",
    url:"http://localhost:8080/Model/Predit",
    data: ModelAndTestName
  })
}

export async function PredictedMultiData(requestBody) {
  return axios({
    method:"post",
    url:"http://localhost:8080/Model/PreditMulti",
    data: requestBody
  })
}

export async function ModelFeature(record) {
  return axios({
    method:"post",
    url:"http://localhost:8080/Model/FeatureImport",
    data: record
  })
}

export async function GetModelFeature(record) {
  return axios({
    method:"post",
    url:"http://localhost:8080/Model/GetFeatureImport",
    data: record
  })
}

export async function ResultFeatureData(record) {
  return axios({
    method:"post",
    url:"http://localhost:8080/Model/ResultFeatureData",
    data: record
  })
}

export async function findById(id) {
  return axios({
    method:"get",
    url:`http://localhost:8080/Model/findById?id=${id}`,
  })
}
